package com.emm.yixun.mobile.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.model.GetCustomerList.CustomerList;

import java.util.ArrayList;

public class CustomerAdapter extends BaseAdapter {

    public boolean isv = false;

    Context context;

    ArrayList<CustomerList> list;

    int starType = 1;

    // int type= 0;

    // /**
    // * 去重
    // *
    // * @param list
    // * @return
    // */
    // public ArrayList<CustomerList> removeDuplicate(ArrayList<CustomerList>
    // list) {
    // Set set = new LinkedHashSet<CustomerList>();
    // set.addAll(list);
    // list.clear();
    // list.addAll(set);
    // return list;
    // }

    public CustomerAdapter(Context context, ArrayList<CustomerList> list) {
        this.context = context;
        this.list = list;
    }

    public ArrayList<CustomerList> getArraylist() {
        return this.list;
    }

    // public void stopTwo(int type) {
    // this.type = type;
    // notifyDataSetChanged();
    // }
    public void SetType(int starType) {
        this.starType = starType;
        notifyDataSetChanged();
    }

    public void SetDate(ArrayList<CustomerList> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addDate(ArrayList<CustomerList> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public boolean GetIsv(boolean isv) {
        this.isv = isv;
        return isv;
    }

    public boolean IsSelect() {
        return getSize() >= (null == list ? 0 : list.size());
    }

    public int getSize() {
        int p = 0;
        if (null == list) {
            return 0;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isIsselect()) {
                p++;
            }
        }
        return p;
    }

    public int GetAllSize() {
        return list == null ? 0 : list.size();
    }

    public void select(int position) {
        if (list.get(position).isIsselect()) {
            list.get(position).setIsselect(false);
        } else {
            list.get(position).setIsselect(true);
        }
        notifyDataSetChanged();
    }

    /**
     * <p>
     * <b>Title：</b> 全选或者全部取消
     * </p>
     * <p>
     * <b>Description：</b>
     * </p>
     *
     * @param isSelect
     * @author tangxin
     * @date 2017年2月20日 下午5:57:02
     */
    public void SelectAll(boolean isSelect) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setIsselect(isSelect);
        }
        notifyDataSetChanged();
    }

    public void SELECTAlone(int position) {
        list.get(position).setIsselect(true);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list == null ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHoudlerCustomer v = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.customer_item2, null);
            v = new ViewHoudlerCustomer();
            v.do_type = (TextView) convertView.findViewById(R.id.do_type);
            v.time = (TextView) convertView.findViewById(R.id.time);
            v.type_view = (TextView) convertView.findViewById(R.id.type_view);
            v.user_name = (TextView) convertView.findViewById(R.id.user_name);
            v.select_image = (ImageView) convertView.findViewById(R.id.select_image);
            v.start_image = (ImageView) convertView.findViewById(R.id.start_image);
            v.lv_name = (TextView) convertView.findViewById(R.id.lv_name);
            v.Belonging_people = (TextView) convertView.findViewById(R.id.Belonging_people);
            convertView.setTag(v);
        } else {
            v = (ViewHoudlerCustomer) convertView.getTag();
        }

        v.Belonging_people.setText(EmmApplication.isNull(list.get(position).getAttributionName()) ? list.get(position)
                .getAttributionName() : "");
        v.do_type.setText(EmmApplication.isNull(list.get(position).getCustomerStatus()) ? list.get(position)
                .getCustomerStatus() : "");
        v.time.setText(EmmApplication.isNull(list.get(position).getCreateTime()) ? list.get(position).getCreateTime() : "");
        v.type_view.setText(EmmApplication.isNull(list.get(position).getCustomerOrigin()) ? list.get(position)
                .getCustomerOrigin() : "");
        v.user_name.setText(EmmApplication.isNull(list.get(position).getCustomerName()) ? list.get(position)
                .getCustomerName() : "");
        v.lv_name.setText((EmmApplication.isNull(list.get(position).getIntentionLevel()) ? "("
                + list.get(position).getIntentionLevel() + ")" : " "));
        if (isv) {
            v.select_image.setVisibility(View.VISIBLE);
        } else {
            v.select_image.setVisibility(View.GONE);
        }

        if (list.get(position).isIsselect()) {
            v.select_image.setImageResource(R.drawable.ico_selt);
        } else {
            v.select_image.setImageResource(R.drawable.ico_delt_no);
        }

        if ("1".equals(list.get(position).getStar())) {
            v.start_image.setVisibility(View.VISIBLE);
        } else {
            v.start_image.setVisibility(starType == 1 ? View.GONE : View.INVISIBLE);
        }

        if ("0".equals(list.get(position).getDataAuthority())) {
            v.do_type.setVisibility(View.INVISIBLE);
            v.lv_name.setVisibility(View.INVISIBLE);
            v.type_view.setVisibility(View.INVISIBLE);
        } else {
            v.do_type.setVisibility(View.VISIBLE);
            v.lv_name.setVisibility(View.VISIBLE);
            v.type_view.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    public static class ViewHoudlerCustomer {

        ImageView select_image, start_image;

        TextView user_name, type_view, do_type, time, lv_name, Belonging_people;

    }

}
